﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PlanningGIS.Util.Convertor;
using System.Text.RegularExpressions;
using PlanningGIS.Util.Arithmetic;

namespace PlanningGIS.Win.Controls
{
    /// <summary>
    /// 度分秒输入控件
    /// </summary>
    public partial class DmsInput : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        private string[] degree = { "度", "°"," " };
        /// <summary>
        /// 
        /// </summary>
        private string[] minmte = { "分", "′", "’"," " };
        /// <summary>
        /// 
        /// </summary>
        private string[] secend = { "秒", "″", "”", " " };

        /// <summary>
        /// 设置控件的值，可以是一个数字，也可以是以符号或者中文形式的字符串，如：120°30′25.6″ 或 120度30分25.6秒
        /// </summary>
        public dynamic Value
        {
            set
            {
                txtClDeg.Text = "";
                txtClMin.Text = "";
                txtClSec.Text = "";

                if (value == null) return;
                if (string.IsNullOrWhiteSpace((string)value)) return;
                string valStr = value.ToString();
                if (StringHelper.CanConvert2Double(value))
                {
                    double num = double.Parse(valStr);
                    int d = (int)num;
                    num = (num - d) * 60;
                    int m = (int)num;
                    double s = (num - m) * 60.0;
                    txtClDeg.Text = d.ToString();
                    txtClMin.Text = m.ToString();
                    txtClSec.Text = s.ToString();
                }
                else
                {
                    valStr = valStr.TrimStart().TrimEnd();
                    bool right = false;
                    for (int i = 0; i < degree.Length; i++)
                    {
                        if(valStr.Contains (degree[i]))
                        {
                            int idx = valStr.IndexOf(degree[i]);
                            txtClDeg.Text = valStr.Substring(0, idx);
                            valStr = valStr.Substring(idx + 1).TrimStart();
                            right = true;
                            continue;
                        }
                    }

                    if (!right) return;
                    right = false;

                    for (int i = 0; i < minmte.Length; i++)
                    {
                        if (valStr.Contains(minmte[i]))
                        {
                            int idx = valStr.IndexOf(minmte[i]);
                            txtClMin.Text = valStr.Substring(0, idx);
                            valStr = valStr.Substring(idx + 1).TrimStart();
                            right = true;
                            continue;
                        }
                    }

                    if (!right) return;
                    if (String.IsNullOrWhiteSpace(valStr)) return;

                    for (int i = 0; i < secend.Length; i++)
                    {
                        if (valStr.EndsWith(secend[i]))
                        {
                            valStr = valStr.Substring(0, valStr.Length - 1);
                            continue;
                        }
                    }
                    txtClSec.Text = valStr;

                }
            }
        }

        /// <summary>
        /// 获取以度为单位的数值
        /// </summary>
        /// <returns></returns>
        public double GetNumber()
        {
            if (txtClDeg.Text == "" && txtClMin.Text == "" && txtClSec.Text == "") return 0;

            try
            {
                double d = txtClDeg.Text == "" ? 0 : double.Parse(txtClDeg.Text);
                double m = txtClMin.Text == "" ? 0 : double.Parse(txtClMin.Text);
                double s = txtClSec.Text == "" ? 0 : double.Parse(txtClSec.Text);

                return CoordSystemHelper.DMS2DEG(d, m, s);
            }
            catch 
            {
                return 0;
            }
        }

        /// <summary>
        /// 获取标点符号格式的字符串，如 120°30′25.6″
        /// </summary>
        /// <param name="zero">如果不填，是否补零</param>
        /// <returns></returns>
        public string GetSymbolString(bool zero = false)
        {
            if (zero)
            {
                return (txtClDeg.Text == "" ? "0" : txtClDeg.Text) + "°" + (txtClMin.Text == "" ? "0" : txtClMin.Text) + "′" + (txtClSec.Text == "" ? "0" : txtClSec.Text) + "″";
            }
            else
            {
                if (txtClDeg.Text == "")
                {
                    return "";
                }
                else
                {
                    string rtn = txtClDeg.Text + "°";
                    if (txtClMin.Text == "")
                    {
                        return rtn;
                    }
                    else
                    {
                        rtn = rtn + txtClMin.Text + "′";
                        if (txtClSec.Text == "")
                        {
                            return rtn;
                        }
                        else
                        {
                            rtn = rtn + txtClSec.Text + "″";
                            return rtn;
                        }

                    }
                }
            }
        }

        /// <summary>
        /// 获取中文汉字格式的字符串，如 120度30分25.6秒
        /// </summary>
        /// <param name="zero">如果不填，是否补零</param>
        /// <returns></returns>
        public string GetCharacterString(bool zero = false)
        {
            if (zero)
            {
                return (txtClDeg.Text == "" ? "0" : txtClDeg.Text) + "度" + (txtClMin.Text == "" ? "0" : txtClMin.Text) + "分" + (txtClSec.Text == "" ? "0" : txtClSec.Text) + "秒";
            }
            else
            {
                if (txtClDeg.Text == "")
                {
                    return "";
                }
                else
                {
                    string rtn = txtClDeg.Text + "度";
                    if (txtClMin.Text == "")
                    {
                        return rtn;
                    }
                    else
                    {
                        rtn = rtn + txtClMin.Text + "分";
                        if (txtClSec.Text == "")
                        {
                            return rtn;
                        }
                        else
                        {
                            rtn = rtn + txtClSec.Text + "秒";
                            return rtn;
                        }

                    }
                }
            }
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public DmsInput()
        {
            InitializeComponent();

            txtClDeg.KeyPress += ControlVerify.txtIntBox_KeyPress;
            txtClMin.KeyPress += ControlVerify.txtIntBox_KeyPress;
            txtClSec.KeyPress += ControlVerify.txtDoubleBox_KeyPress;

        }
        /// <summary>
        /// 
        /// </summary>
        public event EventHandler ValueChanged = null;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtClDeg_TextChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null) ValueChanged(this, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtClMin_TextChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null) ValueChanged(this, null);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtClSec_TextChanged(object sender, EventArgs e)
        {
            if (ValueChanged != null) ValueChanged(this, null);
        }
    }
}
